﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using StickyNote.Core;
using StickyNote.Services;
using StickyNote.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StickyNote.ViewModels
{
    internal class MainWindowViewModel : ObservableObject
    {
        private NoteService fNoteService = Ioc.Default.GetRequiredService<NoteService>();

        private List<NoteWindow> fNoteWindows = new List<NoteWindow>();

        public List<NoteWindow> NoteWindows
        {
            get
            {
                return this.fNoteWindows;
            }

            set
            {
                this.SetProperty(ref this.fNoteWindows, value);
            }
        }

        public MainWindowViewModel()
        {
            this.InitNoteWindows();
            this.NoteWindows = this.fNoteService.NoteWindows.OrderByDescending(p => p.WindowNo).ToList();
        }

        public RelayCommand NewNoteCmd => new RelayCommand(NewNote);

        private void NewNote()
        {
            this.fNoteService.AddNoteWindow();
        }

        private void InitNoteWindows()
        {
            int count = 0;
            string noteNameStr = IniHelper.GetValue(E_Sections.业务.ToString(), E_Keys.便签.ToString());
            if (!string.IsNullOrEmpty(noteNameStr))
            {
                string[] noteNames = noteNameStr.Split(',');
                this.fNoteService.InitNoteWindows(noteNames);
                count = noteNames.Length;
            }

            if (count == 0)
            {
                this.fNoteService.AddNoteWindow();
            }
        }
    }
}
